package apollo.game.core;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Matrix4;
import apollo.game.core.Input;

import apollo.game.scene.ApplictionScene;
import apollo.game.scene.MapScene;
import apollo.game.scene.Scene;

public class GameApplication implements Display, ApplicationListener {
	
	private static int width;
	private static int height;
	private SpriteBatch spriteBatch;
	private Scene current;
	private Input input;

	public GameApplication(int width, int height) {
		GameApplication.width = width;
		GameApplication.height = height;
	}
	
	public static int getWidth() {
		return width;
	}
	public static int getHeight() {
		return height;
	}
	
	@Override
	public void create() {
		Matrix4 projection = new Matrix4();
		projection.setToOrtho(0, 320, 240, 0, -1, 1);

		spriteBatch = new SpriteBatch(100);
		spriteBatch.setProjectionMatrix(projection);
		
		input = new Input();
		Gdx.input.setInputProcessor(input);
		
		MapScene scene = new MapScene();
		this.setCurrent(scene);
	}
	
	@Override
	public void setCurrent(Scene scene) {
		spriteBatch.dispose();
		this.current = scene;
		scene.init();
	}

	@Override
	public Scene getCurrent() {
		return current;
	}
	
	@Override
	public void dispose() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void resume() {
	}

	@Override
	public void render() {
		int fps = Gdx.graphics.getFramesPerSecond();
		if (fps > 0) {
			long wt = (long)(1000 / 60 - Gdx.graphics.getDeltaTime());		
			try {
				Gdx.graphics.wait(wt);
			} catch (InterruptedException e) {
			}
		}
		
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		current.tick(input);
		input.tick();
		current.render(spriteBatch, 0, 0, 320, 240);		
	}
}
